package com.eling.elcms.privilege.webapp.controller;

import com.eling.elcms.core.webapp.controller.BaseFormController;
import com.eling.elcms.core.webapp.controller.Msg;
import com.eling.elcms.privilege.service.IUserFindPwdService;
import com.eling.elcms.system.service.ICaptchaService;
import org.apache.commons.codec.binary.Base64;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
public class UserFindPwdController extends BaseFormController {

	@Autowired
	private IUserFindPwdService userFindPwdService;
	
	@Autowired
	private ICaptchaService captchaService;

	/**
	 * 发送验证码，密码找回
	 */
	@RequestMapping("/api/user/getvalidatecode")
	@ResponseBody
	public Msg sendValidateCode(HttpServletRequest request, HttpServletResponse response) throws Exception {
		return userFindPwdService.sendValidateCode(request, response);

	}

	/**
	 * 校验手机验证码
	 * 
	 * @param request
	 * @param phone
	 * @param vcode
	 * @return
	 */
	@RequestMapping("/api/user/validateCode")
	@ResponseBody
	public Msg validateCode(final HttpServletRequest request, @RequestParam("username") String username,
			@RequestParam("phone") String phone, @RequestParam("validatecode") String vcode) {
		return userFindPwdService.validateCode(request, username, phone, vcode);
	}

	/**
	 * 更新密码
	 */
	@RequestMapping("/api/user/updateuserpwd")
	@ResponseBody
	public Msg updateUserPwd(final HttpServletRequest request,
			@RequestParam(value = "pkUser", required = false) String pkUser,
			@RequestParam(value = "passWord", required = false) String passWord) {
		return userFindPwdService.updateUserPwd(request, pkUser, passWord);
	}

	/**
	 * 发邮件
	 * 
	 */
	@RequestMapping("/api/user/sendfindpwdemail")
	@ResponseBody
	public Msg sendFindPwdEmail(final HttpServletRequest request,
			@RequestParam(value = "email", required = false) String email) {
		return userFindPwdService.sendFindPwdEmail(request, email);
	}
	
	/**
	 * 返回图形验证码
	 * 
	 * @param request
	 * @param response040786
	 * @throws Exception
	 */
	@RequestMapping("/api/user/getCaptcha")
	@ResponseBody
	public Msg getCaptcha(HttpServletRequest request) {
		byte[] buffer = captchaService.genCode(request.getSession().getId(), "findpwd");
		String base64 = Base64.encodeBase64String(buffer);
		return new Msg(base64);
	}
	

	/**
	 * 校验邮件
	 */
	@RequestMapping("/api/user/validatefindpwdemail")
	@ResponseBody
	public ModelAndView validateFindPwdEmail(@RequestParam("pkUser") String pkUser,
			@RequestParam("activetoken") String token, HttpServletRequest req, HttpServletResponse res) {
		return userFindPwdService.validateFindPwdEmail(pkUser, token, req, res);
	}

}
